package com.friend.campusnotice.adapter;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.friend.campusnotice.ContactDetailActivity;
import com.friend.campusnotice.R;
import com.friend.campusnotice.bean.CallLog;
import com.friend.campusnotice.bean.Contacts;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/5/24.
 */
public class CallLogAdapter extends RecyclerView.Adapter<CallLogAdapter.ContactHolder> {
    private Context mContext;
    private List<CallLog> mCallLogList;
    private onItemClickListener onItemClickListener;
    private int mypos ;

    public CallLogAdapter(Context mContext, List<CallLog> callLogList) {
        this.mContext = mContext;
        this.mCallLogList = callLogList;
    }

    public void setOnItemClickListener(CallLogAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    @Override
    public ContactHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ContactHolder holder = new ContactHolder(LayoutInflater.from(mContext)
                .inflate(R.layout.call_log_item_layout, parent, false));
        return holder;

    }

    @Override
    public void onBindViewHolder(final ContactHolder holder, final int position) {

        mypos = position;

        if (mCallLogList.get(position).getName() != null) {
            holder.contactName.setText(mCallLogList.get(position).getName());
            holder.contactFrist.setText(mCallLogList.get(position).getName().substring(mCallLogList.get(position).getName().length() - 1));

        } else {
            holder.contactName.setText(mCallLogList.get(position).getPhoneNum());
            holder.contactFrist.setText(mCallLogList.get(position).getPhoneNum().substring(mCallLogList.get(position).getPhoneNum().length() - 1));
        }

        Date d = new Date(mCallLogList.get(position).getCallDate());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
        holder.callTime.setText(sdf.format(d));
        holder.callNumber.setText(mCallLogList.get(position).getPhoneNum());
        if (mCallLogList.get(position).getCallType().equals("未接")) {
            Log.e("xiaohei","颜色变红了  "+"第"+position+"个item 的类型是 "+ mCallLogList.get(position).getCallType());
            holder.callType.setTextColor(Color.RED);
        }else{
            Log.i("xiaohei","颜色变灰了  "+"第"+position+"个item 的类型是 "+ mCallLogList.get(position).getCallType());
            holder.callType.setTextColor(Color.GRAY);
        }
        holder.callType.setText(mCallLogList.get(position).getCallType());
        Log.i("xiaohei","第"+position+"个item 的类型是 "+ mCallLogList.get(position).getCallType());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    int pos = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView, pos);
                }
            }
        });

        if (mCallLogList.get(position).getSingle()){
            holder.detail.setVisibility(View.GONE);
        }

        holder.detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,ContactDetailActivity.class);
                Bundle bundle = new Bundle();
                Contacts contacts = new Contacts();
                contacts.setPhoneNumber(mCallLogList.get(position).getPhoneNum());
                contacts.setContactName(mCallLogList.get(position).getName());
                bundle.putParcelable("contact",contacts);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });


    }

    @Override
    public int getItemCount() {
        return mCallLogList.size();
    }

    class ContactHolder extends RecyclerView.ViewHolder{
        TextView contactName;
        Button contactFrist;
        TextView callType;
        TextView callTime;
        TextView callNumber;
        RelativeLayout detail;


        public ContactHolder(View view) {
            super(view);
            contactName = (TextView) view.findViewById(R.id.contact_real_name);
            callTime = (TextView) view.findViewById(R.id.call_time);
            callNumber = (TextView) view.findViewById(R.id.call_number);
            callType = (TextView) view.findViewById(R.id.call_type);
            contactFrist = (Button) view.findViewById(R.id.contact_name);
            detail = (RelativeLayout) view.findViewById(R.id.detail_rel);
        }
    }

    public  interface onItemClickListener{
        void onItemClick(View view, int position);
    }

}
